<template>
  <div style="margin-right: 15px">
    <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
      <el-form-item label="所属机构" v-if="!queryParams.orgId">
        <select-org-tree :value="queryParams.orgId" @orgNodeClick="orgNodeClick" />
      </el-form-item>
      <el-form-item label="药品名称" prop="itemInfo">
        <el-input
          v-model="queryParams.itemInfo"
          clearable
          placeholder="请输入药品名称、代码、拼音码"
        />
      </el-form-item>
      <div style="float: right">
        <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
      </div>
    </el-form>
    <el-table
      v-adaptive
      :data="drugInfoList"
      :border="true"
      highlight-current-row
      :stripe="true"
      @row-click="rowClick"
      :header-cell-style="$headerCellStyle"
    >
      <el-table-column
        :show-overflow-tooltip="true"
        :align="'left'"
        label="药品编码"
        prop="drugCode"
        width="130px"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        :align="'left'"
        fixed="left"
        label="药品名称"
        prop="drugName"
        width="180px"
      >
        <template #default="scope">
          <span class="fz14 fw-bold">{{ scope.row.drugName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        :align="'right'"
        label="包装规格"
        prop="packageSpec"
        width="130px"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        :align="'left'"
        label="厂商名称"
        prop="firmName"
        width="260px"
      />
    </el-table>
    <pagination
      v-show="total > 0"
      :layout="layout"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :pager-count="pagerCount"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script lang="ts" setup>
import {selectPageDrugInfo} from "@/api/drug/drugInfo";
import {useUserStoreHook} from "@/store/modules/user";

const { proxy } = getCurrentInstance() as any;
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  drugName: undefined,
  drugInput: undefined,
  drugCode: undefined,
  orgId: userInfo.orgId,
  itemInfo: undefined
});

const props = defineProps({
  orgId: {
    type: String,
    default: "",
  },
});
if (userInfo.orgId) {
  queryParams.value.orgId = userInfo.orgId;
  getList();
}

const emits = defineEmits(["rowClick", "selectAllReght"]);
// 药品字典基本信息表格数据
const drugInfoList = ref([]);
// 总条数
const total = ref(0);
// 列表分页功能点显示
const layout = ref("total, sizes, jumper");
// 页码按钮的数量
const pagerCount = ref(5);
// 查询参数
const queryParamsRef = ref();

// watch(() => props.orgId, (_val, _old) => {
//     getList()
// }, { deep: true, immediate: true })

// if (orgId.value) {
//     queryParams.value.orgId = orgId.value
//     getList()
// }

/** 查询药品字典基本信息列表 */
function getList() {
  if (!queryParams.value.orgId) {
    return ElMessage.error(proxy.$OrgIdIsEmpty);
  }
  selectPageDrugInfo(queryParams.value).then((response: any) => {
    drugInfoList.value = response.rows;
    total.value = response.total;
  });
}

// 机构树点击事件
function orgNodeClick(data, _e) {
  queryParams.value.orgId = data.orgId;
  getList();
  emits("selectAllReght", data.orgId);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields();
  handleQuery();
}
function rowClick(row) {
  emits("rowClick", row);
}
</script>

<script lang="ts">
export default {
  name: "drugInfoTable",
};
</script>

<style scoped></style>
